package com.panyunting.notepad.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.panyunting.notepad.R;
import com.panyunting.notepad.javabean.Note;
import java.util.List;

public class NotePadAdapter extends BaseAdapter {
    private List<Note> noteList;
    private Context mContext;
    //这是adapter的构造方法，数据从外面传进来
    public NotePadAdapter (Context context,List<Note> noteList){
        this.noteList = noteList;
        this.mContext = context;
    }
    @Override
    public int getCount() {
        return noteList.size();
    }

    @Override
    public Object getItem(int position) {
        return noteList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext,R.layout.item_notepad,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Note note = noteList.get(position);
        viewHolder.tvNotepadTime.setText(note.getNoteTime());
        viewHolder.tvNotepadContent.setText(note.getContent());
        return convertView;
    }
    class ViewHolder{
        TextView tvNotepadContent;
        TextView tvNotepadTime;
        public ViewHolder(View view){
            tvNotepadContent = view.findViewById(R.id.tv_content);
            tvNotepadTime = view.findViewById(R.id.tv_notepad_time);
        }
    }
}
